package de.dnb.basics.utils;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.print.PrinterException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import de.dnb.basics.applicationComponents.StreamUtils;
import de.dnb.basics.applicationComponents.StringUtils;

public class HTMLUtils {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String html = StringUtils.readClipboard();
		show(html);
		System.out.println("hi");
	}

	/**
	 * 
	 * @param html				sollte als html formatiert sein.
	 * @throws PrinterException Wenn irgendwas mit Drucker nicht klappt.
	 * @return					true, wenn ausgedruckt wurde, false, wenn
	 * 							abgebrochen wurde.
	 */
	public static boolean print(String html) throws PrinterException {
		/*
		 * Aus 
		 * https://docs.oracle.com/javase/tutorial/uiswing/misc/printtext.html
		 */
		JEditorPane jTextComponent = new JEditorPane();
		jTextComponent.setContentType("text/html");
		jTextComponent.setText(html);
		boolean complete = jTextComponent.print();
		return complete;
	}

	/**
	 * Druckt ohne störende Dialoge
	 * 
	 * @param html				sollte als html formatiert sein.
	 * @throws PrinterException Wenn irgendwas mit Drucker nicht klappt.
	 * @return					true, wenn ausgedruckt wurde, false, wenn
	 * 							abgebrochen wurde.
	 */
	public static boolean printWithoutDialog(String html)
		throws PrinterException {
		/*
		 * Aus 
		 * https://docs.oracle.com/javase/tutorial/uiswing/misc/printtext.html
		 */
		JEditorPane jTextComponent = new JEditorPane();
		jTextComponent.setContentType("text/html");
		jTextComponent.setText(html);
		boolean complete =
			jTextComponent.print(null, null, false, null, null, false);
		return complete;
	}

	/**
	 * 
	 * @param fileName
	 * @throws FileNotFoundException	
	 * @throws PrinterException			Wenn irgendwas mit Drucker nicht klappt.
	 */
	public static void file2Printer(String fileName)
		throws FileNotFoundException,
		PrinterException {
		String s = StreamUtils.readIntoString(new FileInputStream(fileName));
		print(s);
	}

	public static void show(String html) {
		Pattern titlePattern = Pattern.compile("<title>(.*)</title>");
		Matcher matcher = titlePattern.matcher(html);
		String match = "";
		if (matcher.find())
			match = matcher.group(1);
		final String title = match;
		JEditorPane ar = new JEditorPane("text/html", html);
		ar.setEditable(false);

		final JScrollPane scrollpane = new JScrollPane(ar);
		JFrame jFrame = new JFrame(title);
		jFrame.getContentPane().add(scrollpane);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setLocation(300, 100);
		jFrame.setSize(700, 700);
		jFrame.setVisible(true);
	}

	/**
	 * 
	 * @param reference	nicht null
	 * @param text		nicht null
	 * @return			Hyperlink. In der LinkAdresse sind die
	 * 					Blanks durch %20 ersetzt.
	 */
	public static String makeHtmlRef(String reference, String text) {
		String ref = reference.replace(" ", "%20");
		String s;
		s = "<a href=http://" + ref + ">" + text + "</a>";
		return s;
	}

	/**
	 * @param s
	 * @return
	 */
	public static String bold(String s) {
		return "<b>" + s + "</b>";
	}

	/**
	 * @param s
	 * @return
	 */
	public static String italic(String s) {
		s = "<i>" + s + "</i>";
		return s;
	}

	public static String table(String s, int border, int spacing, int padding) {
		return "<table border=\"" + border + "\" cellspacing=\"" + spacing
			+ "\" cellpadding=\"" + padding + "\" valign=\"top\">" + s
			+ "</table>";
	}

	public static String tableLine(String s) {
		return "<tr>" + s + "</tr>";
	}

	public static String tableCell(String s, int fontsize) {
		return "<td style=\"font-family:Arial;font-size:" + fontsize + "pt\">"
			+ s + "</td>";
	}

	public static String heading(String s, int i) {
		return "<h" + i + ">" + s + "</h" + i + ">";
	}

	public static String span(String s) {
		return "<span style=\"font-family:Arial;font-size:16pt\">" + s
			+ "</span>";
	}

}
